package com.ascainiao.paygame.utils;

import com.ascainiao.paygame.controller.ImageCode;
import org.slf4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ascainiao.paygame.beans.ReturnData;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Tools {
    private static final Logger logger = LoggerFactory.getLogger(Tools.class);
    public static JSONObject readJsonFile(String filePath) throws IOException {
        //String userDir = System.getProperty("user.dir");
        //System.out.println(userDir);
        //Resource resource = resourceLoader.getResource("classpath:static/myfile.txt");
        //String filePath = "path/to/your/file.json"; // 替换为你的JSON文件路径
        try (BufferedReader reader = new BufferedReader(new FileReader("E:/phpstudy_pro/WWW"+filePath))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            String jsonString = sb.toString().trim();

            // 将JSON字符串转换为JSONObject
            JSONObject jsonObject = JSON.parseObject(jsonString);
            return jsonObject;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void writeJsonFile(String filePath,String config){
        try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("E:/phpstudy_pro/WWW"+filePath), StandardCharsets.UTF_8)) {
        //try (FileWriter writer = new FileWriter("E:/phpstudy_pro/WWW"+filePath)) {
            writer.write(config);
            //System.out.println("文本已成功写入文件：" + fileName);
        } catch (IOException e) {
            //System.out.println("写入文件时发生错误：" + e.getMessage());
        }
    }
    public static void readImage(String filePath,HttpServletResponse response) throws IOException {
        String ext=getFileExtension(filePath);
        String imagePath="E:/phpstudy_pro/WWW"+filePath;
        File imageFile = new File(imagePath);
        if (!imageFile.exists()) {
            throw new IOException("Image file not found: " + imagePath);
        }
        // 读取图片为BufferedImage对象
        BufferedImage bufferedImage = ImageIO.read(imageFile);
        try {
            // 将内存中的图片通过流动形式输出到客户端
            ImageIO.write(bufferedImage, ext, response.getOutputStream());
        } catch (Exception e) {
            logger.error("将内存中的图片通过流动形式输出到客户端失败>>>>   ", e);
        }
    }
    public static ReturnData output(String status,String message,Object data){
        ReturnData rdata=new ReturnData();
        rdata.setStatusCode(status);
        rdata.setMessage(message);
        rdata.setData(data);
        return rdata;
    }
    public static String getFileExtension(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return null;
        }

        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            return ""; // 没有找到点，返回空字符串
        }

        return fileName.substring(dotIndex + 1);
    }
}
